<template>
  <div class="card" style="margin-bottom: 5px">
    <el-input clearable @clear="load" style="width: 260px; margin-right: 5px;" v-model="data.title" placeholder="请输入标题查询" :prefix-icon="Search"></el-input>
    <el-button type="primary" @click="load">查 询</el-button>
    <el-button type="info" @click="reset">重 置</el-button>
  </div>
  <div class="card"  style="margin-bottom: 5px" v-if="data.user.role === 'USER'">
    <el-button type="primary" @click="headleAdd">提交请假申请</el-button>
  </div>

  <div class="card" style="margin-bottom: 5px">
    <el-table :data="data.tableData" style="width: 100%" :header-cell-style="{ color:'#333',backgroundColor:'#dde8ff'}">
      <el-table-column prop="title" label="请假标题" />
      <el-table-column prop="content" label="请假说明" />
      <el-table-column prop="userName" label="请假人" />
      <el-table-column prop="time" label="提交时间" />
      <el-table-column prop="status" label="审核说明" >
        <template v-slot="scope">
          <el-tag type="warning" v-if="scope.row.status === '待审核'">{{ scope.row.status }}</el-tag>
          <el-tag type="success" v-if="scope.row.status === '已通过'">{{ scope.row.status }}</el-tag>
          <el-tag type="danger" v-if="scope.row.status === '未通过'">{{ scope.row.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="reason" label="审核状态" />
      <el-table-column label="操作" width="100">
        <template #default="scope" v-if="data.user.role === 'USER'">
          <el-button :disabled="scope.row.status !== '待审核'" type="primary" icon="Edit" circle @click="headleEdit(scope.row)" ></el-button>
          <el-button :disabled="scope.row.status !== '待审核'" type="danger" icon="Delete" circle @click="del(scope.row.id)" ></el-button>
        </template>
        <template #default="scope" v-else>
          <el-button :disabled="scope.row.status !== '待审核'" type="primary" @click="headleEdit(scope.row)" >审核</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>

  <div class="care">
    <el-pagination
        v-model:current-page="data.pageNum"
        v-model:page-size="data.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[5, 10, 15]"
        :total="data.total"
        @current-change="load"
        @size-change="load"
    />
  </div>

  <el-dialog v-model="data.formVisible" title="请假信息" destroy-on-close width="30%">
    <el-form ref="formRef" :model="data.form" label-width="80px" style="padding: 20px 30px 10px 0">
      <el-form-item prop="title" label="请假标题" v-if="data.user.role === 'USER'">
        <el-input v-model="data.form.title" autocomplete="off" placeholder="请输入请假标题"/>
      </el-form-item>
      <el-form-item prop="title" label="请假说明" v-if="data.user.role === 'USER'">
        <el-input type="textarea" :rows="3" v-model="data.form.content" autocomplete="off" placeholder="请输入请假说明"/>
      </el-form-item>
      <el-form-item prop="status" label="审核状态" v-if="data.user.role === 'ADMIN'">
        <el-select v-model="data.form.status" placeholder="请选择审核状态" style="width: 115px">
          <el-option
              v-for="time in data.statuss"
              :key="time.id"
              :label="time.status"
              :value="time.status"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="reason" label="审核说明" v-if="data.user.role === 'ADMIN' && data.form.status === '未通过'">
        <el-input type="textarea" :rows="3" v-model="data.form.reason" autocomplete="off" placeholder="请输入未通过说明"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="data.formVisible = false">取消</el-button>
        <el-button type="primary" @click="save">
          保存
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import {Search} from "@element-plus/icons-vue";
import {reactive, ref} from "vue";
import request from "@/utils/request.js";
const formRef = ref()
const emit = defineEmits(["updateUser"])
const data = reactive({
  user: JSON.parse(localStorage.getItem('code_user') || '{}'),
  pageNum: 1,
  pageSize: 5,
  title: '',
  tableData: [],
  total: 6,
  rules: {
    title: [
      {required: true, message: '请填写请假标题', trigger: 'blur'},
      // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
    ],
    content: [
      {required: true, message: '请填写请假内容', trigger: 'blur'},
      { min: 10, max: 99, message: '长度在 10 到 99 个字符', trigger: 'blur' }
    ],
  },
  statuss: [
    {status: '待审核'},
    {status: '已通过'},
    {status: '未通过'},
  ],
})
const load = () =>{
  request.get('apply/selectPage',{
    params:{
      pageNum:data.pageNum,
      pageSize:data.pageSize,
      title:data.title
    }
  }).then(res =>{
    if(res.code === '200'){
      data.tableData =res.data.list
      data.total = res.data.total
      emit("updateUser")
    }else {
      ElMessage.error(res.msg)
    }
  })
}
load()
const reset = () =>{
  data.title = ''
  load()
}

// 修改保存按钮
const headleEdit = (row) =>{
  data.form = JSON.parse(JSON.stringify(row)) // 深拷贝
  data.formVisible = true
}

// 新增按钮
const headleAdd = () =>{
  data.form = {}
  data.formVisible = true
}
const add = () =>{
  request.post('apply/add',data.form).then(res =>{
    if(res.code === '200'){
      ElMessage.success('提交成功等待管理员审核')
      data.formVisible = false
      load()
    }else {
      ElMessage.error(res.msg)
    }
  })
}

//  保存按钮 判断是新增还是修改
const save = () =>{
  formRef.value.validate(valid =>{
    if(valid){
      if(data.form.id){
        update()
      }else {
        add()
      }
    }
  })
}

// 修改编辑按钮
const update = () =>{
  formRef.value.validate(valid => { // formref 是表单的引用 表单验证
    if(!valid){
      return false
    }
    request.put('apply/update',data.form).then(res =>{
      if(res.code === '200'){
        data.formVisible = false
        ElMessage.success('修改成功')
        load()
      }else {
        ElMessage.error(res.msg)
      }
    })
  })
}

// 删除按钮
const del = (id) =>{
  ElMessageBox.confirm('此操作将永久删除该文件, 是否继续?', '删除确认', {type: 'warning',}).then(res => {
    console.log(id)
    request.delete('/apply/delete/'+id).then(res =>{
      if(res.code === '200'){
        ElMessage.success('删除成功')
        load()
      }
    }).catch(() => {
      ElMessage.error(res.msg)
    })
  }).catch(() => {
    ElMessage.info('已取消删除')
  });
}
</script>

<style scoped>

</style>